package com.gravata.enigma.reader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

import com.gravata.enigma.model.Bouquet;
import com.gravata.enigma.model.BouquetService;
import com.gravata.enigma.model.Service;

public class ParserUtil {

    private ArrayList<Bouquet> bouquets=new ArrayList<Bouquet>();
    private ArrayList<Service> services=new ArrayList<Service>();
    private ArrayList<BouquetService> bouquetService=new ArrayList<BouquetService>();

    int count = 0;

    public ParserUtil(String ip) throws IOException {

            bouquets = new ArrayList<Bouquet>();
            URL url = new URL(ip+"/web/getallservices");
            URLConnection con = url.openConnection();
            System.out.println("Connection is : " + con);

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));

            String inputLine;
            String fullStr = "";
            while ((inputLine = reader.readLine()) != null)
                fullStr = fullStr.concat(inputLine + "\n");

            InputStream istream = url.openStream();
            try{
	            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
	                    .newDocumentBuilder();

	            Document doc = builder.parse(istream);

	            doc.getDocumentElement().normalize();


	            NodeList nBouquets = doc.getElementsByTagName("e2bouquet");


	            for (int temp = 0; temp < nBouquets.getLength(); temp++) {
	                Node nBouquet = nBouquets.item(temp);
	                Element eBouquet = (Element) nBouquet;

	                Bouquet bouquet=new Bouquet();

	                bouquet.setName(getTagValue("e2servicename",eBouquet));
	                bouquet.setReference(getTagValue("e2servicereference",eBouquet));
	                bouquet.setOrdem(temp+1);

	                Log.d("PARSEUTIL", bouquet.getName());

	                Element eList=(Element) eBouquet.getElementsByTagName("e2servicelist").item(0);
	                NodeList serviceList= eList.getElementsByTagName("e2service");

	                for(int i = 0; i<serviceList.getLength();i++){
	                    Node nService = serviceList.item(i);

		                if (nService.getNodeType() == Node.ELEMENT_NODE) {

		                    Element eElement = (Element) nService;
		                    Service service=new Service();
		                    service.setName(getTagValue("e2servicename",eElement));
		                    service.setReference(getTagValue("e2servicereference",eElement));
		                    Log.d("PARSEUTIL", "     "+service.getName());
		                    services.add(service);
		                    BouquetService bs=new BouquetService(bouquet,service,i+1);

		                    bouquetService.add(bs);
		                }
	                }

	                bouquets.add(bouquet);
	            }
	           }catch(Exception e){
            }

    }

    public ArrayList<Service> getServices(){
    	return services;
    }
    public ArrayList<Bouquet> getBouquets() {
		return bouquets;
	}
    public ArrayList<BouquetService> getBouquetService() {
		return bouquetService;
	}

    private String getTagValue(String sTag, Element eElement) {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
                .getChildNodes();

        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();

    }

}